package mtt.us.client.views.index;


import java.util.List;

import client.rpc.HibernateRemote;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import mtt.us.client.events.GetModelDataEvent;
import mtt.us.client.events.SetModelDataEvent;
import mtt.us.hmvcFramework.client.hmvc.model.GuiModel;
import hmvc4gwt.client.event.HmvcEvent;

public class IndexModel extends GuiModel {
	
	public void doInit() {
		getController().register(GetModelDataEvent.class, this);
	}

	public void handleEvent(HmvcEvent event) {
		
		if (event instanceof GetModelDataEvent) {
			loadFachbereiche();
		}
		
	}
	
	private void loadFachbereiche() {
		
		HibernateRemote.Util.getInstance().getEntities(false, "mtt.us.domain.Fachbereich", 0, 10, "fachbereichNummer", true, null, null, new String [] { "listStudiengaenge" }, new AsyncCallback() {
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			
			public void onSuccess(Object result) {
				
				if (result instanceof List) {
					
					getController().fireLocalEvent(new SetModelDataEvent(result));
				} else {
					Window.alert("The result is not a List");
				}
				
			}
		});
		
		
	}
}
